import { isEmptyObject } from "../../../utils/utils";

import {request} from '../../../utils/http'

const app = getApp();

interface IConfirmIndexPage {
  staffInfo?: Staff.IStaffItem;
  resDetailData?: {};
  resDetailKeys?: {}[];
  formData?: {}[];
}

interface IConfirmIndexMethods {
  saveFormRef?(data): void;
}

Page<IConfirmIndexPage, IConfirmIndexMethods, any>({
  data: {
    resDetailKeys: [
      { key: "date", label: "预约时间" },
      { key: "serviceNameDesc", label: "预约项目" },
      // { key: "serviceTimDesc", label: "预约时长", desc: "分钟" }
    ],
    resDetailData: {},
    formData: [
      {
        key: "customName",
        label: "客户姓名",
        require: true,
        placeholder: "姓名(必填)",
        value:''
      },
      {
        key: "customMobile",
        label: "手机号",
        require: true,
        type: "text",
        placeholder: "手机号(必填)",
        value:''
      },
      // {
      //   key: "customNum",
      //   label: "到店人数",
      //   require: true,
      //   type: "number",
      //   placeholder: "人数(必填)"
      // },
      // {
      //   key: "subscribeRemarks",
      //   label: "备注",
      //   require: false,
      //   placeholder: "请输入备注 "
      // }
    ]
  },
  onLoad() {
    console.log("app.tempData::", app.tempData);
    const { resStaffInfo, resServiceInfo, resTimeInfo } = app.tempData;
    let userinfo:any = my.getStorageSync({
      key: 'userInfo',
    }).data;
    let formData:any = this.data.formData

    formData[0].value = userinfo.name
    formData[1].value = userinfo.mobile

    console.log(userinfo)
    if (!isEmptyObject(resStaffInfo)) {
      this.setData({
        staffInfo: resStaffInfo
      });
    }
    const serviceTimDesc = this.getTimeDescByServiceInfo(resServiceInfo);
    const serviceNameDesc = this.getNameDescByServiceInfo(resServiceInfo);
    const resDetailData = {
      ...resTimeInfo,
      serviceTimDesc,
      serviceNameDesc
    };
    this.setData({
      resDetailData,
      formData
    });
  },
  getTimeDescByServiceInfo(serviceInfo = []) {
    const timeItems = serviceInfo.sort((x, y) => y.serviceTime - x.serviceTime);
    return timeItems[0].serviceTime;
  },
  getNameDescByServiceInfo(serviceInfo = []) {
    const descMap = [...new Set(serviceInfo.map(item => item.serviceDesc))];
    const nameDesc = descMap.reduce((curStr, desc, index) => {
      return index === 0 ? `${desc}` : `${curStr},${desc}`;
    }, ``);
    return nameDesc;
  },
  // 保存表单实例
  saveFormRef(ref) {
    this.formRef = ref;
  },
  // 预约
  setReservationInfo() {
    // 调用预约接口
    my.navigateTo({
      url: "/pages/reservation/records/index"
    });
    // 预约接口调用成功 清除app.tempData数据
    app.tempData.resStaffInfo = {};
    app.tempData.resServiceInfo = [];
    app.tempData.resTimeInfo = {};
  },
  handleSubmit() {
    this.formRef.checkValue().then(formData => {
      this.customInfo = formData;
      let resDetailData:any = this.data.resDetailData

      request('/appoint', 'post', {
        waiter_id:this.data.staffInfo.staffId,
        appoint_at:resDetailData.date,
        project_id:app.tempData.resServiceInfo[0].serviceId
      }).then((res:any) => {
        if (res.code == 200) {
          console.log(res)
          app.tempData.resStaffInfo = {};
          app.tempData.resServiceInfo = [];
          app.tempData.resTimeInfo = {};
          my.navigateTo({
            url:`/pages/reservation/result/result?status=70&id=${res.data.id}`
          })
        }
      })
      // app
      //   .submitOrderNotifMsg()
      //   .then(code => {
      //     // code
      //     this.setReservationInfo();
      //   })
      //   .catch(() => {
      //     this.setReservationInfo();
      //   });
    });
  }
});
